/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.mailet.daemon;


import com.google.inject.Inject;

/**
 * デーモン
 * @author Aileron
 *
 */
public class MailetDaemon
{
    /**
     * status
     * @author Aileron
     *
     */
    private static class Status
    {
        /**
         * 実行中
         */
        boolean isActive = false;
        
        /**
         * 生存フラグ
         */
        boolean isLiving = true;
    }
    
    /**
     * constractor injection
     * @param listener
     */
    @Inject
    public MailetDaemon(
            final MailetDaemonListener listener,
            final MailetDaemonLogging logging)
    {
        this.status = new Status();
        this.listener = listener;
        this.logging = logging;
    }
    
    /**
     * logging
     */
    private final MailetDaemonLogging logging;
    
    /**
     * status
     */
    private final Status status ;
    
    /**
     * listener
     */
    private final MailetDaemonListener listener;
    
    /**
     * init
     */
    public void init()
    {
        final Thread thread = new Thread()
        {
            @Override
            public void run() 
            {
                try
                {
                    while(status.isLiving)
                    {
                        status.isActive = true;
                        listener.listen();
                        status.isActive = false;
                        sleep(1000);
                    }
                }
                catch (Exception e)
                {
                    status.isActive = false;
                    logging.message(e);
                }
            }
        };
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                status.isLiving=false;
                logging.message("process kill all ...");
                while(status.isActive)
                {
                    logging.message("wait 1 sec ...");
                    try 
                    {
                        sleep(1000);
                    } 
                    catch (InterruptedException e) 
                    {
                        logging.message(e);
                    }
                }
                logging.message("end daemon-process");
            }
        });
        thread.start();
    }
}